﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wei8808Cms.BLL;
using Wei8808Cms.Model;
using Wei8808Cms.WebSite.UI;

namespace Wei8808Cms.WebSite.admin.Product
{
    public partial class ProductMasterForm : BasicPage
    {
        public int pcount;                                   //总条数
        public int page;                                     //当前页
        public readonly int pagesize = 14;                    //设置每页显示的大小

        public string property = "";
        public string keywords = "";

        private readonly ProductBLL _bll = new ProductBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.Params["keywords"]))
            {
                this.keywords = Request.Params["keywords"].Trim();
            }
            if (!string.IsNullOrEmpty(Request.Params["property"]))
            {
                this.property = Request.Params["property"].Trim();
            }

            if (!Page.IsPostBack)
            {
                chkLoginLevel("viewProduct");
                this.RptBind();
            }
        }

        private void RptBind()
        {
            if (!int.TryParse(Request.Params["page"] as string, out this.page))
            {
                this.page = 0;
            }
            var qcmodel = new ProductQcInfo();
            qcmodel.ProductName = keywords;
            qcmodel.Status = property;
            qcmodel.PageSize = pagesize;
            qcmodel.PageIndex = page;
            this.pcount = _bll.GetCount(qcmodel);
            this.lbtnDel.Enabled = this.pcount > 0;
            this.rptList.DataSource = _bll.GetPageList(qcmodel);
            this.rptList.DataBind();
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delProduct");
            var list = new List<ProductInfo>();
            for (var i = 0; i < rptList.Items.Count; i++)
            {
                var id = ((Label)rptList.Items[i].FindControl("lb_id")).Text;
                var cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                if (!cb.Checked) continue;
                var model = new ProductInfo { PId = int.Parse(id) };
                list.Add(model);
            }
            if (_bll.Delete(list))
            {
                JscriptPrint("删除成功！", "ProductMasterForm.aspx", "Success");
            }
            else
            {
                JscriptPrint("删除失败！", "ProductMasterForm.aspx", "Error");
            }
        }

        protected string CombUrlTxt(string _keywords, string _property)
        {
            var strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + Server.UrlEncode(_keywords) + "&");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                strTemp.Append("property=" + _property + "&");
            }
            return strTemp.ToString();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductMasterForm.aspx?" + this.CombUrlTxt(txtKeywords.Text.Trim(), ddlProperty.SelectedValue) + "page=0");
        }

        //设置操作
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
            var model = _bll.GetProductInfo(id);
            switch (e.CommandName.ToLower())
            {
                case "ibtnindexhome":
                    if (model.Status == 1)
                        _bll.UpdateField(id, " Status=0 ");
                    else
                        _bll.UpdateField(id, " Status=1 ");
                    break;
            }
            this.RptBind();
        }
    }
}